﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using app.Models;
using AmuTools;

namespace app.Api
{
    /*
        服务合作流程设置接口
    */
    public class ApiStep : ApiBase
    {
        public ApiStep(Controller ctx) : base(ctx) { }

        #region 自定义请求参数
        // 作者
        int req_service_id { get { return GetIntParam(new ReqIntParamOption { Name = "service_id" }); } }
        int req_allow_demand_upload { get { return GetIntParam(new ReqIntParamOption { Name = "allow_demand_upload" }); } }
        int req_allow_checking_modify { get { return GetIntParam(new ReqIntParamOption { Name = "allow_checking_modify" }); } }
        int req_step_id { get { return GetIntParam(new ReqIntParamOption { Name = "step_id" }); } }
        int req_step_number { get { return GetIntParam(new ReqIntParamOption { Name = "step_number" }); } }
        int req_abutment_id { get { return GetIntParam(new ReqIntParamOption { Name = "abutment_id" }); } }
        int req_demand_member_id { get { return GetIntParam(new ReqIntParamOption { Name = "demand_member_id" }); } }
        int req_service_member_id { get { return GetIntParam(new ReqIntParamOption { Name = "service_member_id" }); } }
        string req_failed_reason { get { return GetStringParam(new ReqStringParamOption { Name = "failed_reason" }); } }
        bool req_is_finished { get { return GetBooleanParam(new ReqBooleanParamOption { Name = "is_finished" }); } }
        
        #endregion

        public void ServiceStep(string action)
        {
            switch (action)
            {
                case "get_steps":
                    {
                        List<Step> steps = Step.GetSteps(req_service_id);
                        ResponseJson(new ListResult<Step> { list = steps });
                        break;
                    }
                case "add":
                    {
                        Step step = new Step {
                            service_id = req_service_id
                        };
                        step.number = G.DB.GetCount<Step>(string.Format("service_id={0}", req_service_id));
                        G.DB.Insert<Step>(step);
                        ResponseJson(new IDResult { id = step.id });
                        break;
                    }
                case "modify":
                    {
                        Step step = G.DB.GetById<Step>(req_id);
                        step.title = req_title;
                        step.content = req_content;
                        step.remark = req_remark;
                        step.appendix_ids = req_appendix_ids;
                        step.allow_checking_modify = req_allow_checking_modify;
                        step.allow_demand_upload = req_allow_demand_upload;
                        G.DB.Update<Step>(step);
                        ResponseJson(new Result());
                        break;
                    }
                case "delete":
                    {
                        Step step = G.DB.GetById<Step>(req_id);
                        G.DB.Delete<Step>(req_id);
                        List<Step> steps = Step.GetSteps(req_service_id);
                        for(int i = 0; i < steps.Count; i++)
                        {
                            steps[i].number = i;
                            G.DB.Update<Step>(steps[i]);
                        }
                        ResponseJson(new Result());
                        break;
                    }
                case "exchange": // 两步互换
                    {
                        int id1 = GetIntParam(new ReqIntParamOption { Name = "id1" });
                        int id2 = GetIntParam(new ReqIntParamOption { Name = "id2" });
                        Step step1 = G.DB.GetById<Step>(id1);
                        Step step2 = G.DB.GetById<Step>(id2);
                        int temp = step1.number;
                        step1.number = step2.number;
                        step2.number = temp;
                        G.DB.Update<Step>(step1);
                        G.DB.Update<Step>(step2);
                        ResponseJson(new Result());
                        break;
                    }
                case "get_step_data": // 对接中的单步骤数据查看
                    {
                        string condition = string.Format("step_id={0} and abutment_id={1}", req_step_id, req_abutment_id);
                        StepData sd = G.DB.Get<StepData>(condition).GetFirstEntity();
                        if(sd == null)
                        {
                            sd = new StepData { step_id = req_step_id, abutment_id = req_abutment_id };
                            G.DB.Insert<StepData>(sd);
                            sd = G.DB.Get<StepData>(condition).GetFirstEntity();
                        }
                        ResponseJson(new ObjectResult { data = sd });
                        break;
                    }
                case "set_step_service_data": // 
                    {
                        if(!req_is_finished)
                        {
                            StepData sd = G.DB.GetById<StepData>(req_id);
                            sd.service_remark = req_remark;
                            sd.service_appendix_ids = req_appendix_ids;
                            G.DB.Update<StepData>(sd);
                        }
                        StepAbutmentData sad = GetStepAbutmentData(req_abutment_id);
                        sad.step_number = req_step_number;
                        if(req_status == 2)
                        {
                            sad.status = 2;
                            sad.failed_reason = req_failed_reason;
                        }
                        else
                        {
                            sad.status = 0;
                        }
                        G.DB.Update<StepAbutmentData>(sad);
                        if(req_abutment_id < 1000000000) // 说明是平台服务
                        {
                            PlateformServiceAbutment psa = G.DB.GetById<PlateformServiceAbutment>(req_abutment_id);
                            if(req_status == 2)
                            {
                                SendMessage(psa.demand_member_id,
                                    string.Format("您申请的平台服务【{0}】有新进展", psa.plateformservice.title),
                                    string.Format("您的申请的平台服务【{0}】在第{1}步审核失败，失败原因：{2}/n请进入对接管理查看详情。", psa.plateformservice.title, sad.step_number + 1, sad.failed_reason)
                                );
                            }
                            else if(req_status == 0)
                            {
                                SendMessage(psa.demand_member_id,
                                    string.Format("您申请的平台服务【{0}】有新进展", psa.plateformservice.title),
                                    string.Format("您的申请的平台服务【{0}】当前处在第{1}步，请进入对接管理查看详情。", psa.plateformservice.title, sad.step_number + 1, sad.failed_reason)
                                );
                            }
                        }
                        else
                        {
                            Abutment abutment = G.DB.GetById<Abutment>(req_abutment_id);
                            if (req_status == 2)
                            {
                                SendMessage( abutment.demand_member_id,
                                    string.Format("您的对接服务【{0}】有新进展", abutment.service.title),
                                    string.Format("您的的对接服务【{0}】在第{1}步审核失败，失败原因：{2}/n请进入对接管理查看详情。", abutment.service.title, sad.step_number + 1, sad.failed_reason)
                                );
                            }
                            else if (req_status == 0)
                            {
                                SendMessage( abutment.demand_member_id,
                                    string.Format("您的对接服务【{0}】有新进展", abutment.service.title),
                                    string.Format("您的对接服务【{0}】当前处在第{1}步，请进入对接管理查看详情。", abutment.service.title, sad.step_number + 1, sad.failed_reason)
                                );
                            }
                        }
                        ResponseJson(new Result());
                        break;
                    }
                case "set_step_demand_data": // 
                    {
                        StepData sd = G.DB.GetById<StepData>(req_id);
                        sd.demand_remark = req_remark;
                        sd.demand_appendix_ids = req_appendix_ids;
                        G.DB.Update<StepData>(sd);
                        StepAbutmentData sad = GetStepAbutmentData(sd.abutment_id);
                        sad.status = 1;
                        G.DB.Update<StepAbutmentData>(sad);
                        if (sd.abutment_id < 1000000000) // 说明是平台服务，暂时先不给管理员发消息
                        {
                        }
                        else
                        {
                            Abutment abutment = G.DB.GetById<Abutment>(sd.abutment_id);
                            SendMessage(abutment.service_member_id,
                                string.Format("您的服务对接【{0}】有新进展", abutment.title),
                                string.Format("您的服务对接【{0}】在第{1}步有来自需求方新的提交，请进入对接管理查看详情。", abutment.title, sad.step_number + 1)
                            );
                        }
                        ResponseJson(new Result());
                        break;
                    }
                default: break;
            }
        }

        public void ServiceStepTemplate(string action)
        {
            switch (action)
            {
                case "save": // 存为模板
                    {
                        StepTemplate st = new StepTemplate { member_id = int.Parse(UserCode), name = req_name };
                        G.DB.Insert<StepTemplate>(st);
                        List<Step> list = G.DB.Get<Step>(string.Format("service_id={0}", req_service_id)).GetFirstTableList();
                        foreach(Step step in list)
                        {
                            step.template_id = st.id;
                            step.service_id = 0;
                            G.DB.Insert<Step>(step);
                        }
                        ResponseJson(new Result());
                        break;
                    }
                case "delete":
                    {
                        G.DB.Delete<StepTemplate>(req_id);
                        ResponseJson(new Result());
                        break;
                    }
                case "copy": // 选择模板，会做一个模板的拷贝版本
                    {
                        G.DB.DeleteByCondition<Step>(string.Format("service_id={0}", req_service_id));
                        StepTemplate st = G.DB.GetById<StepTemplate>(req_id);
                        List<Step> list = G.DB.Get<Step>(string.Format("template_id={0}", st.id)).GetFirstTableList();
                        foreach (Step step in list)
                        {
                            step.service_id = req_service_id;
                            step.template_id = 0;
                            G.DB.Insert<Step>(step);
                        }
                        ResponseJson(new Result());
                        break;
                    }
                case "list": // 模板列表
                    {
                        List<StepTemplate> list = G.DB.Get<StepTemplate>(string.Format("member_id={0}", int.Parse(UserCode))).GetFirstTableList();
                        ResponseJson(new ListResult<StepTemplate>{ list = list });
                        break;
                    }
            }
        }

        private StepAbutmentData GetStepAbutmentData(int abutment_id)
        {
            StepAbutmentData sad = G.DB.GetById<StepAbutmentData>(abutment_id);
            if (sad == null)
            {
                sad = new StepAbutmentData { abutment_id = abutment_id, step_number = req_step_number };
                G.DB.Insert<StepAbutmentData>(sad);
            }
            return sad;
        }
    }


}